1. Replacing an Exception
Having seen how easy it is to use exception handling policies, we'll now look at how you can
implement exception shielding by replacing an exception with a different
exception. This approach is also useful if you need to perform cleanup
operations in your code, and then use the exception to expose only what
is relevant. To configure this scenario, simply create a policy in the
same way as the previous example, but with a Replace handler instead of a Wrap handler, as shown in Figure 1.
When you call the method that generates an exception, you see the
same generic exception message as in the previous example. However,
there is no inner exception this time. If you run the example
Behavior After Applying Exception Shielding with a Replace
Handler, the Exception Handling block replaces the original
exception with the new one specified in the exception handling policy.
This is the result:
Exception type System.Exception was thrown.
Message: 'Application Error. Please contact your administrator.'
Source: 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling'
No Inner Exception
2. Logging an Exception
The previous section shows how you can perform exception shielding
by replacing an exception with a new sanitized version. However, you now
lose all the valuable debugging and testing information that was
available in the original exception. Of course, the Librarian (remember
him?) realized that you would need to retain this information and make
it available in some way when implementing the Exception Shielding
pattern. You preserve this information by chaining exception handlers
within your exception handling policy. In other words, you add a
Logging handler to the policy.
That doesn't mean that the Logging
handler is only useful as part of a chain of handlers. If you only want
to log details of an exception (and then throw it or ignore it,
depending on the requirements of the application), you can define a
policy that contains just a Logging
handler. However, in most cases, you will use a Logging handler with other handlers that wrap or
replace exceptions.
Figure 2 shows what happens
when you add a Logging handler to your
exception handling policy. The configuration tool automatically adds the
Logging Application block to the configuration with a set of default
properties that will write log entries to the Windows Application Event
Log. You do, however, need to set a few properties of the Logging exception handler in the Exception Handling Settings section:
Specify the ID for the log event your code will generate as
the Event ID property.
Specify the TextExceptionFormatter as the type of
formatter the Exception Handling block will use. Click the ellipsis
(…) button in the Formatter TypeTextExceptionFormatter in the type selector
dialog that appears. property and select
Set the category for the log event. The Logging block contains
a default category named General, and
this is the default for the Logging exception handler. However, if
you configure other categories for the Logging block, you can select
one of these from the drop-down list that is available when you
click on the Logging Category
property of the Logging
handler.
The configuration tool adds new exception handlers to the end of the handler chain by
default. However, you will obviously want to log the details of the
original exception rather than the new exception that replaces it. You
can right-click on the Logging handler and use the shortcut menu to move it up to the
first position in the chain of handlers if required.
In addition, if you did not already do so, you must add a
reference to the Logging Application block assembly to your project and
(optionally) add a using statement to
your class, as shown here.
using Microsoft.Practices.EnterpriseLibrary.Logging;
Now, when the application causes an exception, the global
exception handler continues to display the same sanitized error message.
However, the Logging handler captures details of the original exception before
the Exception Handling block policy replaces it, and writes
the details to whichever logging sink you specify in the configuration
for the Logging block. The default in this example is Windows
Application Event Log. If you run the example Logging an
Exception to Preserve the Information it Contains, you will
see an exception like the one in Figure 3.
This example shows the Exception Handling block using the default
settings for the Logging block. The Logging block is
extremely configurable. So you can arrange for the Logging handler in your exception handling policy to write
the information to any Windows Event Log, an e-mail message, a database,
a message queue, a text file, a Windows Management Instrumentation (WMI)
event, or a custom location using classes you create that take advantage
of the application block extension points.